﻿using System;

using Gonte.Caching;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyUtilitiesTest
{
    /// <summary>
    ///This is a test class for CacheTest and is intended
    ///to contain all CacheTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CacheTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void CacheWithNullPolicyTest()
        {
            Cache cache = new Cache(null);
        }

        [TestMethod()]
        public void CacheWithNotNullPolicyTest()
        {
            Cache cache = new Cache(new NoExpirationPolicy());

            cache.Add("1", "Data 1");
            Assert.AreEqual(1, cache.Count);

            cache["1"] = "Data 2";
            Assert.AreEqual(1, cache.Count);

            Assert.AreEqual("Data 2", cache["1"]);

            cache.Remove("1");
            Assert.AreEqual(0, cache.Count);
        }

        [TestMethod()]
        public void CacheAddTest()
        {
            ExpirationPolicy policy = new LruExpirationPolicy(3); // Will expire when we add 4 items
            Cache cache = new Cache(policy);
            Assert.AreEqual(cache, policy.Cache);
            
            cache.Add("1", "Data 1");
            Assert.AreEqual(1, cache.Count);

            cache.Add("2", "Data 2");
            Assert.AreEqual(2, cache.Count);

            cache.Add("3", "Data 3");
            Assert.AreEqual(3, cache.Count);

            cache.Add("4", "Data 4");
            // A this time "Data 1" should be gone
            Assert.AreEqual(3, cache.Count);
            Assert.IsNull(cache["1"]);
        }

        [TestMethod()]
        public void CacheAddWithIndexerTest()
        {
            ExpirationPolicy policy = new LruExpirationPolicy(3); // Will expire when we add 4 items
            Cache cache = new Cache(policy);
            Assert.AreEqual(cache, policy.Cache);

            cache["1"] = "Data 1";
            Assert.AreEqual(1, cache.Count);

            cache["2"] = "Data 2";
            Assert.AreEqual(2, cache.Count);

            cache["3"] = "Data 3";
            Assert.AreEqual(3, cache.Count);

            cache["4"] = "Data 4";
            // A this time "Data 1" should be gone
            Assert.AreEqual(3, cache.Count);
            Assert.IsNull(cache["1"]);
        }

        [TestMethod()]
        public void CacheAddWithFetchTest()
        {
            ExpirationPolicy policy = new LruExpirationPolicy(3); // Will expire when we add 4 items
            Cache cache = new Cache(policy);
            Assert.AreEqual(cache, policy.Cache);

            cache.Add("1", "Data 1");
            Assert.AreEqual(1, cache.Count);

            cache.Add("2", "Data 2");
            Assert.AreEqual(2, cache.Count);

            cache.Add("3", "Data 3");
            Assert.AreEqual(3, cache.Count);

            Assert.AreEqual("Data 1", cache["1"]); // This causes 1 to go to the first position

            cache.Add("4", "Data 4");
            // A this time "Data 2" should be gone
            Assert.AreEqual(3, cache.Count);
            Assert.IsNull(cache["2"]);
        }

        [TestMethod()]
        public void CacheAddWithReplaceTest()
        {
            ExpirationPolicy policy = new LruExpirationPolicy(3); // Will expire when we add 4 items
            Cache cache = new Cache(policy);
            Assert.AreEqual(cache, policy.Cache);

            cache.Add("1", "Data 1");
            Assert.AreEqual(1, cache.Count);

            cache.Add("2", "Data 2");
            Assert.AreEqual(2, cache.Count);

            cache.Add("3", "Data 3");
            Assert.AreEqual(3, cache.Count);

            cache["1"] = "Data 5"; // This causes 1 to go to the first position

            cache.Add("4", "Data 4");
            // A this time "Data 2" should be gone
            Assert.AreEqual(3, cache.Count);
            Assert.IsNull(cache["2"]);
        }

        [TestMethod()]
        public void CacheAddWithRemoveTest()
        {
            ExpirationPolicy policy = new LruExpirationPolicy(3); // Will expire when we add 4 items
            Cache cache = new Cache(policy);
            Assert.AreEqual(cache, policy.Cache);

            cache.Add("1", "Data 1");
            Assert.AreEqual(1, cache.Count);

            cache.Add("2", "Data 2");
            Assert.AreEqual(2, cache.Count);

            cache.Add("3", "Data 3");
            Assert.AreEqual(3, cache.Count);

            cache["1"] = "Data 5"; // This causes 1 to go to the first position

            cache.Add("4", "Data 4");
            // A this time "Data 2" should be gone
            Assert.AreEqual(3, cache.Count);
            Assert.IsNull(cache["2"]);

            cache.Remove("3");
            Assert.AreEqual(2, cache.Count);

            cache.Add("5", "Data 5");
            Assert.AreEqual(3, cache.Count);
            Assert.AreEqual("Data 5", cache["5"]);
        }
    }
}
